Conditions | 9 |
Paths | 33 |
Total Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* TODO |
||
116 | item.sem = escaper.unescape(element.eq(2).text()); |
||
117 | item.regular = escaper.unescape(element.eq(4).text()); |
||
118 | item.exam = escaper.unescape(element.eq(5).text()); |
||
119 | item.overall = escaper.unescape(element.eq(6).text()); |
||
120 | |||
121 | if (title in ret.grades) { |
||
122 | if (item.overall < ret.grades[title].overall) { |
||
123 | return; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | ret.grades[title] = item; |
||
128 | ret['subject-count']++; |
||
129 | |||
130 | // 暂不考虑NaN,(字符串 < 60)为false |
||
131 | if (parseInt(item.overall) < 60) { |
||
132 | ret.failed[title] = item; |
||
133 | ret['failed-count']++; |
||
134 | } |
||
135 | }); |
||
136 | |||
137 | access.logout(headers, res, function() { |
||
138 | // 返回JSON |
||
139 | res.send(JSON.stringify(ret)); |
||
140 | fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green); |
||
1 ignored issue
–
show
|
|||
141 | }); |
||
142 | }); |
||
143 | |||
144 | |||
145 | }); |
||
146 | }); |
||
147 | |||
148 | // 查考试API,通过GET传入用户名和密码 |
||
149 | View Code Duplication | app.get('/exams', function (req, res, next) { |
|
150 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) { |
||
151 | res.send({ error: "参数不正确" }); |
||
152 | return; |
||
153 | } |
||
154 | if (fullLog) { |
||
155 | var start = new Date(); |
||
156 | console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow); |
||
1 ignored issue
–
show
|
|||
157 | } |
||
158 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) { |
||
159 | fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
1 ignored issue
–
show
|
|||
160 | |||
161 | var ret = {}; |
||
162 | var $ = cheerio.load(ires.text); |
||
163 | |||
164 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
165 | ret.id = req.query.id; |
||
166 | ret.sem = req.query.sem || getSem(); |
||
167 | |||
168 | superagent |
||
169 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list') |
||
170 | .set(headers) |
||
171 | .type('form') |
||
172 | .send({ |
||
173 | xqlbmc: '', |
||
174 | xnxqid: ret.sem, |
||
175 | xqlb: '' |
||
176 | }) |
||
177 | .end(function (err, iires) { |
||
178 | if (err) { |
||
179 | console.log((timeStamp() + 'Failed to reach exams page\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
180 | res.send({ error: '获取成绩失败' }); |
||
181 | return next(err); |
||
182 | } |
||
183 | fullLog && console.log((timeStamp() + 'Successfully entered exams page.').green); |
||
184 | |||
185 | $ = cheerio.load(iires.text); |
||
186 | |||
187 | ret.exams = {}; |
||
188 | ret['exams-count'] = 0; |
||
189 | |||
216 |